package com.youmingxing.view;

import android.content.Context;
import android.content.DialogInterface;
import android.widget.TextView;

import com.youmingxing.R;
import com.youmingxing.dialog.LodingDialog;

/**
 * @author evilyin(ChenZhixi)
 * @since 15/10/28
 */
public class DialogManager {
    private static LodingDialog loadingDialog;

    public static void showDialog(Context context, String msg) {
        if (loadingDialog == null) {
            loadingDialog = LodingDialog.createDialog(context);
            loadingDialog.setCanceledOnTouchOutside(false);
            loadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    dialog.dismiss();
                }
            });
        }
        if (msg != null) {
            TextView tvMsg = (TextView) loadingDialog.findViewById(R.id.id_tv_loadingmsg);
            if (tvMsg != null) {
                tvMsg.setText(msg);
            }
        } else {
            TextView tvMsg = (TextView) loadingDialog.findViewById(R.id.id_tv_loadingmsg);
            if (tvMsg != null) {
                tvMsg.setText("");
            }
        }
        loadingDialog.show();
    }

    public static void showDialog(Context context) {
        showDialog(context, "加载中");
    }

    public static void showForceDialog(Context context, String msg) {
        if (loadingDialog == null) {
            loadingDialog = LodingDialog.createDialog(context);
            loadingDialog.setCancelable(false);
            loadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    dialog.dismiss();
                }
            });
        }
        TextView tvMsg = (TextView) loadingDialog.findViewById(R.id.id_tv_loadingmsg);
        if (tvMsg != null) {
            tvMsg.setText(msg);
        }
        loadingDialog.show();
    }

    public static void dismissDialog() {
        if (loadingDialog != null) {
            if (loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }
            loadingDialog = null;
        }
    }

//    public static void showSuccessToast(String)

}
